package com.ykdl.member.kid.fragments;

import java.util.List;

import com.lidroid.xutils.BitmapUtils;
import com.ykdl.member.kid.R;
import com.ykdl.member.kid.adapters.HeatImageListAdpater;
import com.ykdl.member.kid.circle.TopicAndResultActivity;
import com.ykdl.member.kid.gears.FreakyChatChildListActivity;
import com.ykdl.member.kid.gears.HeatImageListActivity;
import com.ykdl.member.kid.gears.HomeActivity;
import com.ykdl.member.kid.image.ImageOptionsFactory;
import com.ykdl.member.kid.models.CatalogBean;
import com.ykdl.member.kid.models.FileMetas;
import com.ykdl.member.kid.models.HomeBean;
import com.ykdl.member.kid.models.PostActor;
import com.ykdl.member.kid.models.TopicBean;
import com.ykdl.member.kid.util.MobileUtils;
import com.ykdl.member.kid.widget.AsyncImageView;
import com.ykdl.member.views.MyGridView;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.RelativeLayout;
import android.widget.RelativeLayout.LayoutParams;
import android.widget.TextView;
import android.widget.Toast;

public class HeatImageListFragment extends Fragment {
	private HomeActivity  activity;
	private MyGridView gv_heat_image_list;
	private List<TopicBean> topics;
	private RelativeLayout rl_heat_image_title;
	private int gradview_width;
	private int screenWidth;
	private int gradview_item_width;
	private int gradview_item_image_height;
	
	private BitmapUtils  bitmapUtil;
	private HeatImageListAdpater adapter;
	@Override
	public void onAttach(Activity activity) {
		super.onAttach(activity);
		this.activity = (HomeActivity)activity;
		this.bitmapUtil = new BitmapUtils(this.activity);
		
	}
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View  view = LayoutInflater.from(activity).inflate(R.layout.heat_image_list_view, null);
		rl_heat_image_title = (RelativeLayout) view.findViewById(R.id.rl_heat_image_title);
		gv_heat_image_list = (MyGridView) view.findViewById(R.id.gv_heat_image_list);
		
		return view;
		
	}
	
	@SuppressWarnings("unchecked")
	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		Bundle  bundle = this.getArguments();
		 final  HomeBean  homeBean = (HomeBean) bundle.getSerializable("homebean");
		 topics = homeBean.getTopics();
		 
		/**设置适配**/
		 screenWidth = MobileUtils.getScreenWidthIntPx();
		 gradview_width = (screenWidth - MobileUtils.dpToPx(12));
		 gradview_item_width = gradview_width / 3; 
		 gradview_item_image_height = (int)((gradview_item_width - MobileUtils.dpToPx(12)) / 0.97); //计算每个item的高度
		
		LayoutParams  params = (LayoutParams) gv_heat_image_list.getLayoutParams();
		params.width = gradview_width;
		params.rightMargin = MobileUtils.dpToPx(6);
		params.leftMargin = MobileUtils.dpToPx(6);
		
		gv_heat_image_list.setHorizontalSpacing(MobileUtils.dpToPx(12));
		gv_heat_image_list.setVerticalSpacing(MobileUtils.dpToPx(12));
		gv_heat_image_list.setColumnWidth(gradview_item_width);
		gv_heat_image_list.setCacheColorHint(getResources().getColor(R.color.touming));
		gv_heat_image_list.setNumColumns(3);
		
		//设置适配器
		adapter = new HeatImageListAdpater(this.activity, this.bitmapUtil, gradview_item_image_height);
		adapter.setList(topics);
		gv_heat_image_list.setAdapter(adapter);
		
		gv_heat_image_list.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				/**单个话题，进入话题终端页*/
				final TopicBean topic = topics.get(arg2);
				if(topic !=null){
				Intent intent =new Intent(activity,TopicAndResultActivity.class);
				intent.putExtra(TopicAndResultActivity.STR_TOPIC_ID, topic.getTopic_id());
				intent.putExtra(TopicAndResultActivity.STR_GROUP_ID, topic.getSource_id());
				activity.startActivity(intent);
				}
			}
			
		});
		
		rl_heat_image_title.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				Intent t = new Intent(activity,  HeatImageListActivity.class);
				startActivity(t);
			}
		});
	}

}
